Add Tokamax ring MHA attention for TPU#4266
Merged
copybara-service[bot] merged 4 commits intoJul 17, 2026
Merged
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
huytransformer
force-pushed
the
htn/tokamax-ring-tpu
branch
from
June 25, 2026 07:09
050abe5 to
fb361d0
Compare
huytransformer
force-pushed
the
htn/tokamax-ring-tpu
branch
from
June 25, 2026 16:17
fb361d0 to
9b610fb
Compare
huytransformer
marked this pull request as ready for review
June 26, 2026 04:24
huytransformer
requested review from
A9isha,
NicoGrande,
NuojCheng,
RissyRan,
SurbhiJainUSC,
abhinavclemson,
aireenmei,
bvandermoon,
darisoy,
dipannita08,
gagika,
gobbleturk,
hengtaoguo,
igorts-git,
jacoguzo,
jiangjy1982,
khatwanimohit,
parambole,
richjames0,
shralex,
shuningjin,
suexu1025 and
vipannalla
as code owners
June 26, 2026 04:24
huytransformer
force-pushed
the
htn/tokamax-ring-tpu
branch
from
June 26, 2026 20:33
ce68360 to
f02b0bc
Compare
NuojCheng
reviewed
Jul 6, 2026
NuojCheng
reviewed
Jul 6, 2026
NuojCheng
reviewed
Jul 6, 2026
NuojCheng
reviewed
Jul 6, 2026
NuojCheng
reviewed
Jul 6, 2026
NuojCheng
reviewed
Jul 6, 2026
NuojCheng
reviewed
Jul 6, 2026
NuojCheng
reviewed
Jul 6, 2026
NuojCheng
reviewed
Jul 6, 2026
huytransformer
force-pushed
the
htn/tokamax-ring-tpu
branch
3 times, most recently
from
July 10, 2026 21:30
45e5f89 to
37b508c
Compare
NuojCheng
reviewed
Jul 10, 2026
NuojCheng
reviewed
Jul 10, 2026
NuojCheng
reviewed
Jul 10, 2026
NuojCheng
reviewed
Jul 10, 2026
NuojCheng
reviewed
Jul 10, 2026
NuojCheng
reviewed
Jul 10, 2026
NuojCheng
reviewed
Jul 10, 2026
NuojCheng
approved these changes
Jul 10, 2026
NuojCheng
left a comment
Collaborator
There was a problem hiding this comment.
Overall LGTM. I am a little scared by the scale of cloned codes. I have two suggestions on editing the PR description:
- Please specify reasons why cloning tokamax ring/splash and making local changes are necessary;
- Please provide example maxtext command for running ring attention, with performance comparison, see example description of PR#2783, also add link to your doc.
…kamax-ring-tpu # Conflicts: # src/maxtext/configs/pyconfig_deprecated.py # src/maxtext/layers/attention_op.py # tests/unit/configs_value_test.py
huytransformer
force-pushed
the
htn/tokamax-ring-tpu
branch
from
July 11, 2026 07:52
37b508c to
0eccf59
Compare
gobbleturk
reviewed
Jul 14, 2026
gobbleturk
approved these changes
Jul 14, 2026
copybara-service
Bot
merged commit Jul 17, 2026
a375072
into
AI-Hypercomputer:main
86 of 90 checks passed
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds TPU support for
context_parallel_strategy=ringin Flash attention. This PR uses and adapts some Tokamax Splash components and includes the MaxText integration, config validation, and sharding checks.Key Changes:
context_parallel_strategy=ringnow runs on TPU (previously GPU only). Q remains sequence sharded while KV rotate around the context mesh axis, partial results are combined with online softmax.src/maxtext/kernels/tokamax_splash_attention/, using Tokamax 0.0.12 (openxla/tokamax@4936e75). This replaces the previous imports fromtokamax._src.ops.experimental...and provides the necessary modifications the ring path needs.tokamax_ring_attention.pymodule owns the MaxText/Tokamax interface such as mesh-axis derivation, sharding checks (ring axis only on the sequence dim, GQA/MQA head mapping preserved, head/dKV dims local),SplashConfigconstruction, and the sharded ring kernel call.Key files:
src/maxtext/kernels/attention/tokamax_ring_attention.py: This detects when ring CP is requested, derives Q/KV mesh axes, validates ring-mesh/head/dKV sharding, builds the TokamaxSplashConfigand lazy causal mask, and wraps the kernel withmake_sharded_ring_attention_kernel/call_ring_attention.src/maxtext/kernels/tokamax_splash_attention/(adapted from Tokamax):splash_attention_kernel.py: the Splash kernel, adapted to provide log-sum-exp and max logits, converted from base-2 to natural base, and to share types viabase.py.ring_attention_kernel.py: the ring forward/backward over KV shards adapted to factor reusable logic intoring_attention_utils.pyand to validate the ring-axis size against the built kernel.ring_attention_utils.py: shared ring helpers such asMaskInfoslicing, Q-sequence offsets per KV shard, block-activity checks.splash_attention_mask.py/splash_attention_mask_info.py: mask classes and block-metadata construction, adapted for slicing mask metadata per ring step.base.py: shared types (SegmentIds, residuals, mask constants).src/maxtext/layers/attention_op.py: adds mesh/sharding validation when ring is requested, rejects features the ring path does not support like sinks, indexer masks, ragged attention, chunked prefill, bidirectional masks, max-logit recording...; routes the flash path to the sharded ring kernel when ring is requested.src/maxtext/configs/types.py: acceptscontext_parallel_strategy=ringon TPU and enforces the supported combination likeattention=flash+use_tokamax_splash=True, rejects packing, CP load balancing, ragged attention, sinks, indexer, chunked prefill, MoBA, multimodal, QK-clip, and dropout with explicit errors.docs/guides/optimization/sharding.md: ring CP is now supported on TPU via the Tokamax Splash path (in addition to the GPU Transformer Engine path).tests/unit/tokamax_ring_attention_test.py: ring output and gradients vs Splash reference.tests/unit/tokamax_splash_attention_mask_test.py: tests for the adapted mask components.tests/unit/attention_test.py: e2eAttentionOpring-CP test under a CP mesh.tests/unit/configs_value_test.py: validation cases for the TPU ring.Limitation and Future work
Performance comparison
Run command
Example on a v5p-16 (8 chips) and 64K context:
Tests
Ran:
Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.